const app = require('../../live.js')
Page({
  data: {
    showLoadingB: true, 
    avatar: '',
    nick: '',
    chatroomList: [],
    chatroomImageBaseUrl: '',
    pageSize: 10,
    pageNumber: 1,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },

  // 跳转到去直播页面
  toLive: function (e) {
    let netId = e.currentTarget.dataset.netid
    wx.navigateTo({
      url: '../cover-title/cover-title?netId=' + netId
    })
  },
  

  onLoad(options) {
    var that = this
    if (that.data.showLoadingB){
      wx.showLoading({
        title: '加载中',
        mask: true
      })
    }
    
    // 查看是否授权
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success(res) {
              var encryptedData = res.encryptedData
              var iv = res.iv
              var code = ''
              wx.login({
                success: res => {
                  code = res.code;
                  wx.request({
                    url: app.pageConfig.jnbyUrl + '/sunday/small/member/loginWithUnion',
                    method: 'POST',
                    header: {
                      "Content-Type": "application/x-www-form-urlencoded"
                    },
                    data: {
                      "code": code,
                      'encryptedData': encryptedData,
                      'iv': iv
                    },
                    success: function (res) {
                      if (res.data.code != 0) {
                        wx.redirectTo({
                          url: '../login/login'
                        })
                      } else {
                        var memberId = res.data.result.id
                        var netId = res.data.result.netId
                        var userType = res.data.result.userType
                        that.setData({
                          memberId: memberId,
                          netId: netId,
                          userType: userType,
                          showLoadingB: false
                        })
                        setTimeout(function () {
                          wx.hideLoading()
                        },1000)
                        
                      }
                    }
                  })
                }
              })
            }
          })
        } else {
          wx.redirectTo({
            url: '../login/login'
          })
        }
      }
    })

    let pageConfig = app.pageConfig
    const userInfo = app.userInfo
    let nickName = '匿名'
    let avatarUrl = pageConfig.defaultAvatar
    if (userInfo) {
      nickName = userInfo.nickName
      avatarUrl = userInfo.avatarUrl
    }
    this.setData({
      avatar: avatarUrl,
      nick: nickName,
      chatroomImageBaseUrl: pageConfig.chatroomImageBaseUrl
    })
    wx.request({
      url: pageConfig.chatroomListUrl,
      header: {
        appkey: pageConfig.appkey,
      },
      success: (res) => {
        const { data } = res
        if (data.res === 200) {
          this.setData({
            chatroomList: data.msg.list
          })
          app.chatroomList = [...data.msg.list]
        }
      }
    })

  },
  switchToChatRoom(e) {
    var that = this;
    let roomId = e.currentTarget.dataset.roomid
    let memberId = e.currentTarget.dataset.memberid
    let roomName = e.currentTarget.dataset.name
    let id = e.currentTarget.id
    let status = e.currentTarget.dataset.status
    let onlineNum = e.currentTarget.dataset.onlinenum
    let zanNum = e.currentTarget.dataset.zannum
    let accountImage = e.currentTarget.dataset.accountimage
    let accountId = e.currentTarget.dataset.accountid
    let accountName = e.currentTarget.dataset.accountname
    let src = e.currentTarget.dataset.src
    let playSrc = e.currentTarget.dataset.playsrc
    let url = encodeURIComponent(e.currentTarget.dataset.pushurl)
    let integeral = e.currentTarget.dataset.integeral
    let scoreTime = e.currentTarget.dataset.scoretime
    if (app.inChatroom == true) {
      wx.showToast({
        title: '当前网络不佳，上次连接尚未断开',
        icon: 'none',
        duration: 2000
      })
    } else {
      console.log('status'+status)
      if (status === 1) {
        if (that.data.netId == accountId){
          wx.navigateTo({
            url: `../push/push?url=${url}`
          })
        } else {
          wx.navigateTo({
            url: '../live-room/live-room?roomId=' + roomId + '&liveId=' + id + '&name=' + roomName + '&memberId=' + memberId + '&integeral=' + integeral + '&scoreTime=' + scoreTime + '&accountImage=' + accountImage + '&accountName=' + accountName + '&src=' + src,
            success: function () {
              app.inChatroom = true
            }
          })
        }
        
      } else {
        wx.navigateTo({
          url: '../video-room/video-room?roomId=' + roomId + '&liveId=' + id + '&name=' + roomName + '&memberId=' + memberId  + '&scoreTime=' + scoreTime + '&accountImage=' + accountImage + '&accountName=' + accountName + '&src=' + playSrc,
          success: function () {
            app.inChatroom = true
          }
        })
      }
      
    }
  },

  /**
  * 生命周期函数--监听页面显示
  */
  onShow: function () {
    var that = this
    that.setData({
      pageNumber: 1
    })

    // 获取房间列表
    that.getRoomList()
  },

  
  // 获取房间列表
  getRoomList: function () {
    let that = this
    wx.request({
      url: app.pageConfig.jnbyUrl +'/sunday/small/live/select',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        pageSize: that.data.pageSize,  //返回数据的个数  
        pageNumber: that.data.pageNumber, //返回数据的第几页
        sort: 'ct',
        order: 'desc'
      },
      success: function (res) {
        let roomList = res.data.result
        console.log(roomList)
        that.setData({
          roomList: roomList
        })
      },
      complete: function () {
        wx.stopPullDownRefresh()
      }
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this

    that.setData({
      pageNumber: 1
    })

    // 获取房间列表
    that.getRoomList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let pageNumber = this.data.pageNumber
    pageNumber = pageNumber + 1
    this.setData({
      pageNumber: pageNumber
    });

    let that = this
    // console.log("123"+that.data.memberId)
    wx.request({
      url: app.pageConfig.jnbyUrl +'/sunday/small/live/select',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        pageSize: that.data.pageSize,  //返回数据的个数  
        pageNumber: that.data.pageNumber, //返回数据的第几页
        sort: 'ct',
        order: 'desc'
      },
      success: function (res) {
        let roomList = that.data.roomList.concat(res.data.result)
        // console.log('请求了')
        that.setData({
          roomList: roomList
        })
      }
    })
  },

  /**
    * 用户点击右上角分享
    */
  onShareAppMessage: function () {
    return {
      title: '江南布衣',
      path: '/pages/live-list/live-list'
    }
  }
})
